package com.sansecy.customview.view.calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.sansecy.customview.view.calendar.bean.SignDateBean;
import com.sansecy.customview.view.calendar.calendarhelper.CalendarConstant;
import com.sansecy.customview.view.calendar.calendarhelper.CalendarModel;
import com.sansecy.customview.view.calendar.calendarhelper.CalendarUtil;
import com.sansecy.customview.view.calendar.calendarhelper.LunarCalendar;
import com.sansecy.customview.view.calendar.calendarhelper.SolarTermUtil;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * @Description 月日历视图
 * @Date 2019-08-20 23:04
 * @Created by caoyu
 */
public class MonthCalendarView extends View {

    public static final String SIGNED = "signed";
    public static  int DATE_SIZE = 20;
    public static  int LUNAR_SIZE = 11;
//    private Paint mWeekPaint;
    private Paint mDefaultPaint;
    private Paint mHighLightPaint;
    private List<CalendarModel> mCalendarModels;
    private int mCurrentMonth;
    private int mCurrentDay;
    private Paint mSchemePaint;
    private Paint mLunarPaint;
    private float mLunarAscent;
    private int mSchemeRatio = 5;

    public MonthCalendarView(Context context) {
        this(context, null);
    }

//    private static int COLOR_RED = Color.parseColor("#f2523e");

    public MonthCalendarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

//        mWeekPaint = new Paint();
//        mWeekPaint.setColor(Color.BLACK);
//        mWeekPaint.setAntiAlias(true);
//        mWeekPaint.setTextSize(sp2px(DATE_SIZE));

        SolarTermUtil.init(getContext());
        LunarCalendar.init(getContext());

        java.util.Calendar instance = java.util.Calendar.getInstance(Locale.getDefault());
        int year = instance.get(java.util.Calendar.YEAR);
        mCurrentMonth = instance.get(java.util.Calendar.MONTH) + 1;
        mCurrentDay = instance.get(java.util.Calendar.DAY_OF_MONTH);
        CalendarModel calendarModel = new CalendarModel();
        calendarModel.setYear(year);
        calendarModel.setMonth(mCurrentMonth);
        calendarModel.setDay(mCurrentDay);
        mCalendarModels = CalendarUtil.initCalendarForMonthView(year, mCurrentMonth, calendarModel, CalendarConstant.WEEK_START_WITH_MON);

        //这个日历默认返回6行
        //取倒数第7个，判断是否不是当月，不是当月的话，直接去掉后面7天，给绘图留出空间

        CalendarModel model = mCalendarModels.get(mCalendarModels.size() - 8);
        if (model.getMonth() != mCurrentMonth) {
            for (int i = 0; i < 7; i++) {
                mCalendarModels.remove(mCalendarModels.size() - 1);
            }
            DATE_SIZE = 20;
            mSchemeRatio = 6 ;
        }

        mDefaultPaint = new Paint();
        mDefaultPaint.setColor(Color.parseColor("#dddddd"));
        mDefaultPaint.setTextAlign(Paint.Align.CENTER);
        mDefaultPaint.setAntiAlias(true);
        mDefaultPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mDefaultPaint.setTextSize(sp2px(DATE_SIZE));

        mSchemePaint = new Paint();
        mSchemePaint.setStyle(Paint.Style.FILL);
        mSchemePaint.setAntiAlias(true);
        mSchemePaint.setColor(Color.RED);
        mSchemePaint.setStrokeWidth(5);

        mHighLightPaint = new Paint();
        mHighLightPaint.setColor(Color.RED);
        mHighLightPaint.setTextAlign(Paint.Align.CENTER);
        mHighLightPaint.setAntiAlias(true);
        mHighLightPaint.setTextSize(sp2px(DATE_SIZE));

        mLunarPaint = new Paint();
        mLunarPaint.setAntiAlias(true);
        mLunarPaint.setTextAlign(Paint.Align.CENTER);
        mLunarPaint.setColor(Color.parseColor("#999999"));
        mLunarPaint.setTextSize(sp2px(LUNAR_SIZE));
        mLunarAscent = Math.abs(mLunarPaint.ascent()) + Math.abs(mLunarPaint.descent());

    }

    int itemHeight;
    int count;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //取得日历天数
        int size = mCalendarModels.size();
        //计算总共多少行
        int totalRows = size / 7;
        //取余看是不是多出几个
        if (size % 7 > 0) {
            //多出了再加一行
            totalRows++;
        }
        //得到视图宽度，分成7份
        int itemWidth = getWidth() / 7;
        //取每一份的一半，在这条线上画东西
        int halfItemWidth = itemWidth / 2;
        //定义每一块区域的高度
        int itemHeight = getHeight() / totalRows;
        //取中间值
        int halfItemHeight = itemHeight / 2;
        //根据宽高计算半径
        int radius = Math.max(halfItemWidth, halfItemHeight) / 10 * mSchemeRatio;

        int row = 0;
        //表示当前画到第几个了
        int position = 0;

        for (int i = 0; i < totalRows; i++) {
            //画横线
            for (int j = 0; j < 7; j++) {
                //画竖线
                if (position > size - 1) {
                    return;
                }

                CalendarModel calendarModel = mCalendarModels.get(position);
                String scheme = calendarModel.getScheme();

                if (calendarModel.getMonth() != mCurrentMonth) {
                    if (scheme != null && scheme.equals(SIGNED)) {
                        canvas.drawCircle(halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i, radius, mSchemePaint);
                        mHighLightPaint.setColor(Color.WHITE);
                        mLunarPaint.setColor(Color.WHITE);
                        //绘制阳历
                        canvas.drawText(String.valueOf(calendarModel.getDay()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i, mHighLightPaint);
                        //绘制农历
                        canvas.drawText(String.valueOf(calendarModel.getLunar()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i + mLunarAscent, mLunarPaint);
                    } else {
                        mDefaultPaint.setTextSize(sp2px(DATE_SIZE));
                        //不是当前月，文字显示灰色
                        canvas.drawText(String.valueOf(calendarModel.getDay()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i, mDefaultPaint);
                        //绘制农历
                        mDefaultPaint.setTextSize(sp2px(LUNAR_SIZE));
                        canvas.drawText(String.valueOf(calendarModel.getLunar()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i + mLunarAscent, mDefaultPaint);
                    }
                } else {
                    if (scheme != null && scheme.equals(SIGNED)) {
                        canvas.drawCircle(halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i, radius, mSchemePaint);
                        mHighLightPaint.setColor(Color.WHITE);
                        mLunarPaint.setColor(Color.WHITE);
                        //绘制阳历
                        canvas.drawText(String.valueOf(calendarModel.getDay()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i, mHighLightPaint);
                        //绘制农历
                        canvas.drawText(String.valueOf(calendarModel.getLunar()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i + mLunarAscent, mLunarPaint);
                    } else {
                        mHighLightPaint.setColor(Color.BLACK);
                        mLunarPaint.setColor(Color.BLACK);
                        canvas.drawText(String.valueOf(calendarModel.getDay()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i, mHighLightPaint);
                        //绘制农历
                        canvas.drawText(String.valueOf(calendarModel.getLunar()), halfItemWidth + itemWidth * j, halfItemHeight + itemHeight * i + mLunarAscent, mLunarPaint);
                    }
                    //当前月，文字默认黑色
                }
                position++;
            }
            row++;
        }
    }

    public float sp2px(float size) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, getResources().getDisplayMetrics());
    }

    /**
     * dp转px
     *
     * @param dpValue dp
     * @return px
     */
    private int dpToPx(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void setSignedDays(List<SignDateBean> dates) {
        if (dates == null || dates.size() == 0) {
            return;
        }
        for (int i = 0; i < dates.size(); i++) {
            SignDateBean signDateBean = dates.get(i);
            java.util.Calendar calendar = java.util.Calendar.getInstance(Locale.getDefault());
            calendar.setTimeInMillis(signDateBean.getCheckinTime());
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            for (int j = 0; j < mCalendarModels.size(); j++) {
                CalendarModel model = mCalendarModels.get(j);
                if (year == model.getYear() && month == model.getMonth() && day == model.getDay()) {
                    model.setScheme(SIGNED);
                    break;
                }
            }
        }
        invalidate();
    }
}
